<template>
  <Modal v-model="modal" title="修改密码" @on-ok="ok">
    <Form :model="formItem" label-position="left" :label-width="100">
      <FormItem label="旧密码">
        <Input type="password" v-model="formItem.old_pwd"></Input>
      </FormItem>
      <FormItem label="新密码">
        <Input type="password" v-model="formItem.pwd1"></Input>
      </FormItem>
      <FormItem label="确认密码">
        <Input type="password" v-model="formItem.pwd2"></Input>
      </FormItem>
    </Form>
  </Modal>
</template>
<script setup>
import { reactive } from 'vue'
import http from '@/util/http'
import { JSEncrypt } from 'encryptlong'
const router = useRouter()
let { proxy } = getCurrentInstance()
let modal = ref(false)
const formItem = reactive({ pwd1: '', pwd2: '' })
const modal_show = () => {
  modal.value = true
}

const ok = () => {
  const jsencrypt = new JSEncrypt()
  jsencrypt.setPublicKey(import.meta.env.VITE_PUBLICKEY)
  let en_old_pwd = jsencrypt.encryptLong(formItem.old_pwd)
  let en_pwd1 = jsencrypt.encryptLong(formItem.pwd1)
  let en_pwd2 = jsencrypt.encryptLong(formItem.pwd2)
  http.post('/Basic/User/UpdatePwd', { pwd1: en_pwd1, pwd2: en_pwd2, old_pwd: en_old_pwd }, (response) => {
    if (response.data.flag) {
      proxy.$Message.success(response.data.msg)
      sessionStorage.removeItem('token')
      sessionStorage.removeItem('tenant')
      router.push('/')
    } else {
      proxy.$Message.error(response.data.msg)
    }
  })
}

defineExpose({
  modal_show,
})
</script>